package com.android.tiango.baba.act;

import java.util.Collections;
import java.util.List;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.android.scsd.tiango.R;
import com.android.tiango.baba.BaBaseActivity;
import com.android.tiango.baba.chosephoto.ImageManager2;
import com.android.tiango.baba.entity.PicModel;
import com.android.tiango.baba.util.PhotosSearch;
import com.android.tiango.baba.util.PicCompare;
import com.android.tiango.baba.util.ToastUtils;
import com.android.tiango.baba.view.AppTitleBar;

/**
 * com.image.service.MainActivity
 * 
 * @author YW
 * 
 */
public class ActPhotos extends BaBaseActivity {
	private PhotosSearch imageService;
	private List<PicModel> images;
	private Handler handler;
	private ListView listview;
	private ProgressDialog dialog;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.page_photolist);
		imageService = new PhotosSearch(this);
		dialog = new ProgressDialog(this);
		dialog.setMessage("正在读取照片......");
		dialog.show();
		listview = (ListView) this.findViewById(R.id.lv_pic);
		mTitleBar = (AppTitleBar) findViewById(R.id.titlebar);
		mTitleBar.setTitle("相册");
		setBackOnClickListener();
		this.handler = new Handler() {
			@Override
			public void handleMessage(Message msg) {
				dialog.dismiss();
				if (images.size() > 0) {
					final Myadapter adapter = new Myadapter(ActPhotos.this);
					listview.setAdapter(adapter);
					listview.setOnItemClickListener(new OnItemClickListener() {
						@Override
						public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
							PicModel model = (PicModel) adapter.getItem(arg2);
							if (null != model) {
								Intent intent = new Intent(ActPhotos.this, Actlbum.class);
								intent.putExtra("path", model.getPicParentPath());
								ActPhotos.this.startActivity(intent);
								finish();
							}
						}
					});
				} else {
					ToastUtils.showMessage("没有图片");
				}
			}
		};
		new Thread(new Runnable() {
			@Override
			public void run() {
				allScan();
				images = imageService.getImages();
				Collections.sort(images, new PicCompare());
				handler.sendEmptyMessage(0);
			}
		}).start();
	}

	public void allScan() {
		sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"
				+ Environment.getExternalStorageDirectory())));
	}

	class Myadapter extends BaseAdapter {
		private LayoutInflater inflater;

		public Myadapter(Context context) {
			this.inflater = LayoutInflater.from(context);
		}

		@Override
		public int getCount() {
			return images.size();
		}

		@Override
		public PicModel getItem(int position) {
			return images.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View view, ViewGroup parent) {
			ViewHolder holder = null;
			if (view == null) {
				holder = new ViewHolder();
				view = inflater.inflate(R.layout.item_photos, null);
				holder.imageName = (TextView) view.findViewById(R.id.name);
				holder.imageInfo = (TextView) view.findViewById(R.id.size);
				holder.image = (ImageView) view.findViewById(R.id.image);
				view.setTag(holder);
			} else {
				holder = (ViewHolder) view.getTag();
			}
			PicModel model = (PicModel) getItem(position);
			if (null != model) {
				holder.imageName.setText(model.getPicName());
				// holder.imageInfo.setText("(" + model.getPicNum() + ")");//
				String path = model.getPicPath();
				ImageManager2.from(ActPhotos.this).displayImage(holder.image, path,
						R.drawable.img_loading_default, 100, 100);
			}
			return view;
		}

	}

	class ViewHolder {
		private TextView imageName;
		private TextView imageInfo;
		private ImageView image;
	}

}